import 'package:cloud_medicine/components/address_list.dart';
import 'package:cloud_medicine/http/address_api.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/model/user_address.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:left_scroll_actions/leftScroll.dart';

class AddressListPage extends StatefulWidget {
  AddressListPage({Key key}) : super(key: key);

  @override
  _AddressListPageState createState() => _AddressListPageState();
}

class _AddressListPageState extends State<AddressListPage> {
  bool _loading = false;
  List<AddressItem> addressList = [];

  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });

    dynamic res = await getAddressList_api('$backIP/address/list'); //接口路径
    res['data'].forEach((dynamic addressItem) {
      //遍历接口中的数据
      AddressItem addressItemModel = AddressItem.init(addressItem);
      addressList.add(addressItemModel);
    });
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
    return;
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  Future _deleteAddress(AddressItem e) async {
    await deleteAddress_api('$backIP/address', {"id": e.id});
  }

  _childLayout() {
    if (_loading) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return ListView(
        children: [
          Column(
            children: addressList.map((e) {
              return LeftScroll(
                  buttons: <Widget>[
                    LeftScrollItem(
                      text: '删除',
                      color: Colors.red,
                      onTap: () {
                        setState(() {
                          addressList.remove(e);
                        });
                        _deleteAddress(e);
                      },
                    ),
                  ],
                  child: Container(
                    color: Colors.white,
                    padding: EdgeInsets.all(10),
                    child: Address(
                      e: e,
                    ),
                  ));
            }).toList(),
          ),
          Center(
            child: Container(
              margin: EdgeInsets.only(top: 10),
              child: Text(
                '滑动可删除地址~',
                style: TextStyle(color: Colors.grey[500]),
              ),
            ),
          )
        ],
      );
    }
  }

  // 下拉刷新
  Future<Null> _refresh() async {
    setState(() {
      addressList.clear(); // 清除当前状态数据
    });
    dynamic res = await getAddressList_api('$backIP/address/list'); //接口路径
    res['data'].forEach((dynamic addressItem) {
      //遍历接口中的数据
      AddressItem addressItemModel = AddressItem.init(addressItem);
      setState(() {
        addressList.add(addressItemModel);
      });
    });
    Fluttertoast.showToast(
        msg: '刷新成功',
        backgroundColor: Colors.grey[300],
        textColor: Colors.lightBlueAccent);

    return;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        title: Text('收货地址'),
      ),
      body: RefreshIndicator(
        onRefresh: _refresh,
        color: Theme.of(context).primaryColor,
        displacement: 40, // 指示器显示时距顶部位置
        child: _childLayout(),
      ),
      floatingActionButton: Padding(
        padding: EdgeInsets.only(left: 30),
        child: AddAddress(),
      ),
    );
  }
}
